﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Collections.Specialized;
using devtm.Editor.Configuration.Templates;
using devtm.Configuration.Models;
using System.Collections;
using devtm.Editor.Configuration.Data.SimpleTemplates;

namespace devtm.Editor.Configuration.Data
{
    

    public class ItemPair
    {
        public string Key { get; set; }
        public SimpleProperty Value { get; set; }
        public List<ItemPair> Parent { get; set; }
    }


    public class TemplateNameValueCollection : ConfigProperty
    {

        public List<ItemPair> Liste { get; set; }
        public TemplateNameValueCollection() { }

        public TemplateNameValueCollection(PropertyInfo propertyinfo, ElementSection instance)
            : base(propertyinfo, instance)
        {


            // récupère les attributs
            Hashtable tb = new Hashtable();
            foreach (PropertyItemAttribute item in propertyinfo.GetPropertyItemAttribute(instance))
                tb.Add(item.Name, item);

            Liste = new List<ItemPair>();
            if (Value is NameValueCollection)
            {

                NameValueCollection ii = (NameValueCollection)Value;

                foreach (string item in ii.AllKeys)
                {
                    var i = new ItemPair() { Key = item , Value = new StringSimpleProperty() { TypeContainer = typeContainer.NameValueCollection, ElementSection = instance, Container = ii } };
                    i.Value.Parent = i;

                    i.Value.Template = tb[item] as PropertyItemAttribute;

                    if (i.Value.Template == null)
                    {
                        // on ne trouve pas de template associé a la clef.
                    

                    }

                    i.Value.Value = ii[item];

                    i.Parent = Liste;
                    Liste.Add(i);

                    // Parent = ItemPair
                }

            }
            else if (Value is StringCollection)
            {

                StringCollection ii = (StringCollection)Value;

                foreach (string item in ii)
                {
                    var i = new ItemPair() { Key = item, Value = new StringSimpleProperty() { TypeContainer = typeContainer.StringCollection, ElementSection = instance, Value = item, Container = ii } };
                    i.Value.Parent = i;

                    i.Value.Template = tb[item] as PropertyItemAttribute;
                    i.Value.Value = item;

                    i.Parent = Liste;
                    Liste.Add(i);
                }
            }
            else
            {
                // not Implemented
            }
        }

        public new String Type { get { return "(" + Instance.ConfigurationElement.GetType().FullName + ")"; } }

        //public new Object Value { get { return devtm.Editor.Configuration.Properties.Resources.ThisTypeIsManaged; } }

        /// <summary>
        /// Runs the editor.
        /// </summary>
        public override void RunEditor() { }


        public override void Selector() { }




    }
}
